package com.education.demouse.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.CellData;
import com.education.demouse.pojo.model.StudentAnswerDetails;
import com.education.demouse.service.IStudentAnswerDetailsService;
import com.google.common.collect.Lists;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author: liftsail
 * @Date: 2022/10/17 09:34
 * @Description: 不积跬步无以至千里
 */
@Data
@Component
public class ExcelListener<T> extends AnalysisEventListener<T> {

    //日志输出
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelListener.class);

    //单条数据
    private T data;

    //从工作簿中读到的数据列表
    private List<T> dataList = Lists.newArrayList();

    private String genericName;

    private String genericNameAll;

    private Integer lineNum = 1;

    private static IStudentAnswerDetailsService studentAnswerDetailsService;


    @Autowired
    private ExcelListener(IStudentAnswerDetailsService studentAnswerDetailsService) {
        ExcelListener.studentAnswerDetailsService = studentAnswerDetailsService;
    }

    /**
     * 解析每条数据时都会调用
     */
    @Override
    public void invoke(T data, AnalysisContext context) {
        try {
            //通用方法数据校验
            ExcelImportValid.valid(data);
        } catch (ExceptionCustom e) {
            //在easyExcel监听器中抛出业务异常
            LOGGER.info(("第" + lineNum + "行" + e.getMessage()));
            throw new ExcelAnalysisException("第" + lineNum + "行" + e.getMessage());
        }

        //一行一行读取数据到list集合中
        dataList.add(data);
        lineNum++;
    }

    //表头字段信息
    @Override
    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
        //System.out.println("CellData:" + headMap);
    }

    /**
     * 所有数据解析完之后调用
     * 业务逻辑处理整合到集合中的数据
     * 数据插入时开启事务，抛出异常回滚
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (dataList != null && dataList.size() > 0) {
            for (StudentAnswerDetails studentAnswerDetails : (List<StudentAnswerDetails>) dataList) {
                studentAnswerDetailsService.insertStudentAnswerDetails(studentAnswerDetails);
            }
        }
        // 可以在此处执行业务操作
        // 本例就打印到控制台即可，表示读取完成
        //UserEntity t = (UserEntity) dataList.get(0);
        //LOGGER.info(JSON.toJSONString(t));

        //userEntityMapper.insert((UserEntity) dataList.get(0));
        dataList.clear();
    }

    public void genericType(T data) {
        if (data != null) {
            try {
                genericName = data.getClass().getSimpleName();
                genericNameAll = data.getClass().getTypeName();
                this.genericName = genericName;
                this.genericNameAll = genericNameAll;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
